124B - Permutations - CodeForces Solution


brute force combinatorics implementation *1400

Please click on ads to support us..

Python Code:

from itertools import permutations

n, k = map(int, input().split())
ans = 100000000
l = [input() for _ in range(n)]
p = []
for i in range(n):
    s = ''
    p.append([int(s.join(k)) for k in permutations(l[i])])
for i in range(len(p[0])):
    maxn = 0
    minn = 100000000
    for j in range(len(p)):
        if p[j][i] > maxn:
            maxn = p[j][i]
        if p[j][i] < minn:
            minn = p[j][i]
    if ans > maxn - minn:
        ans = maxn - minn
print(ans)

C++ Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
    ll n, k; cin >> n >> k;
    char m[8][8], ind[8];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < k; j++)
            cin >> m[i][j];
    for(int i = 0; i < k; i++) 
        ind[i] = i;
    ll rpta = 1e9+10;
    do{
        ll mx = 0, mn = 1e9+10; 
        for (int i = 0; i < n; i++) {
            ll x = 0;
            for (int j = 0; j < k; j++) {
                x = x * 10 + (m[i][ind[j]]-'0');
            }
            // cout<<":>"<<x<<":asd\n";
            mx = max(x, mx);
            mn = min(x, mn);
        }
        rpta = min(rpta, mx - mn);
    }
    while(next_permutation(ind,ind + k));
    cout << rpta;
    
}


Comments

Submit
0 Comments
More Questions

Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!